package com.bignerdranch.android.expresshelper.Dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.bignerdranch.android.expresshelper.Bean.ExpressBean;
import com.bignerdranch.android.expresshelper.Utils.ExpressDataHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/29.
 */
public class ExpressDao {
    private ExpressDataHelper mDataHelper;
    private SQLiteDatabase mDatabase;

    public ExpressDao(Context context) {
        mDataHelper = new ExpressDataHelper(context);
        mDatabase = mDataHelper.getReadableDatabase();
    }
    public void insert(ExpressBean bean){

        ContentValues values = new ContentValues();
        values.put("expressNum",bean.getExpressNum());
        values.put("expressName",bean.getExpressName());
        values.put("expressIcon",bean.getExpressIcon());
        values.put("expressStatus",bean.getExpressStatus());
        values.put("expressNick",bean.getExpressNick());

        mDatabase.insert("expressInfo",null,values);

    }

    /**
     * 查询单条数据
     * @param num
     * @return
     */

    public ExpressBean query(String num){

        String sql = "select * from expressInfo where expressNum=?";
        Cursor cursor = mDatabase.rawQuery(sql,new String[]{num});

        while (cursor.moveToNext()){
            String name = cursor.getString(cursor.getColumnIndex("expressName"));
            String icon = cursor.getString(cursor.getColumnIndex("expressIcon"));
            String status = cursor.getString(cursor.getColumnIndex("expressStatus"));
            String nick = cursor.getString(cursor.getColumnIndex("expressNick"));

            ExpressBean bean = new ExpressBean(num,name,icon,status,nick);
            return bean;
        }
        return null;

    }

    public List<ExpressBean> queryAll(){
        String sql = "select * from expressInfo";
        Cursor cursor = mDatabase.rawQuery(sql,null);

        List<ExpressBean> list = new ArrayList<>();

        while (cursor.moveToNext()){

            String num = cursor.getString(cursor.getColumnIndex("expressNum"));
            String name = cursor.getString(cursor.getColumnIndex("expressName"));
            String icon = cursor.getString(cursor.getColumnIndex("expressIcon"));
            String status = cursor.getString(cursor.getColumnIndex("expressStatus"));
            String nick = cursor.getString(cursor.getColumnIndex("expressNick"));
            ExpressBean bean = new ExpressBean(num,name,icon,status,nick);
            list.add(bean);
        }
        /*mDatabase.endTransaction();
        mDatabase.close();*/
        return list;
    }
    public void delete(String num){
         String  sql  = "delete from expressInfo where expressNum = "+num;

        mDatabase.execSQL(sql);

       // mDatabase.delete("expressInfo","expressNum=?",new String[]{num});
       /* mDatabase.endTransaction();

        mDatabase.close();*/
    }
    public void deleteAll(){
       /*  String  sql  = "delete from expressinfo where expressno = "+expressNo;
        database.execSQL(sql);*/

        mDatabase.delete("expressInfo",null,null);


    }
    public void update(String num,String expressIcon){

       /* String  sql = "update expressinfo set companyname = '申通' where expressno = '"+ expressNo+"'";

        database.execSQL(sql);*/
        ContentValues values = new ContentValues();
        values.put("expressIcon",expressIcon);
        mDatabase.update("expressInfo",values,"expressNum=?",new String[]{num});
    }
      public void updateStatus(String num,String expressStatus){

       /* String  sql = "update expressinfo set companyname = '申通' where expressno = '"+ expressNo+"'";

        database.execSQL(sql);*/
        ContentValues values = new ContentValues();
        values.put("expressStatus",expressStatus);
        mDatabase.update("expressInfo",values,"expressNum=?",new String[]{num});
    }



}
